<?php
/**
 * Description: 检测订单是否可以被支付
 * User: devforma
 * Date: 16/1/7
 * Time: 21:09
 */

class OrderCanPay implements iController {

    private $now = 0;

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $cmsdb;

        if (validateUser($queryArgs) !== 1)
            return genOutput(1000);

        //参数校验
        if (empty($queryArgs['order_id']) || !filter_var($queryArgs['order_id'], FILTER_VALIDATE_INT)) {
            return genOutput(1000);
        }

        $this->now = time();

        $order = $cmsdb->getRow('SELECT * FROM `original_orders` WHERE `order_id`=:id AND `user_id`=:uid',
            array(':id' => $queryArgs['order_id'], ':uid' => $queryArgs['user_id']));

        if (empty($order)) {
            return genOutput(1000);
        }

        //检测是否已购买
        $buyed = $cmsdb->getVar('SELECT `id` FROM `relation_user_product` WHERE `user_id`=:uid AND `product_id`=:pid
        AND `product_type`=:ptype', array(
            ':uid' => $order->user_id,
            ':pid' => $order->product_id,
            ':ptype' => $order->product_type
        ));

        if ($buyed)
            return genOutput(1000);

        $canPay = true;
        $code = 0;

        //计算价格
        switch ($order->product_type) {
            case 'zhiboke':
                $personsNum = $cmsdb->getVar('SELECT COUNT(*) FROM `relation_user_product` WHERE `product_type`="zhiboke" AND `product_id`=:pid',
                    array(':pid' => $order->product_id));

                $exist = $cmsdb->getRow('SELECT `id`, `price`, `course_name` AS name, `student_limit`, `time_can_purchase`, `end_day`, `is_limited` FROM `data_live_course` WHERE `id`=:cid',
                    array(':cid' => $order->product_id));

                //购课人数限制
                if (empty($exist) || ($exist->student_limit != -1 && $exist->student_limit <= $personsNum)) {
                    $canPay = false;
                    $code = 8002;
                }

                //抢购课程禁止超数
                if ($exist->is_limited) {
                    $validOrderNum = getValidOrderNum($exist->id, date('Y-m-d H:i:s', $this->now)); //未支付的有效订单数

                    if ($exist->student_limit != -1 && $exist->student_limit < $personsNum + $validOrderNum) {
                        $canPay = false;
                        $code = 8002;
                    }
                }

                //订单是否过期
                if ($order->invalid_time && $order->invalid_time < date('Y-m-d H:i:s', $this->now)) {
                    $canPay = false;
                    $code = 8003;
                }

                //未开售课程禁止提前购买
                if ($exist->time_can_purchase != '0000-00-00 00:00:00' && strtotime($exist->time_can_purchase) > $this->now) {
                    $canPay = false;
                }

                //下架的课程禁止购买
                $endTime = $this->getCourseEndTime($exist->id, $exist->end_day);
                if (strtotime($endTime) < $this->now) {
                    $canPay = false;
                }

                break;

            case 'course':
                $exist = $cmsdb->getRow('SELECT `id`, `current_price` AS price, `course_name` AS name
                FROM `data_vod_course` WHERE `id`=:cid', array(':cid' => $order->product_id));
                break;

            case 'interview':
                $exist = $cmsdb->getRow('SELECT `id`, `product_price` AS price, `product_name` AS name
                FROM `conf_product` WHERE `id`=:id', array(':id' => $order->product_id));
                break;

            //没有该种类型商品
            default:
                return genOutput(1000);
        }

        if (empty($exist)) //没有该商品
            return genOutput(1000);

        return genOutput(1, array(
            'can_pay' => $canPay,
            'code' => $code
        ));
    }

    /**
     * 获取课程真实下架时间
     *
     * @param $courseId
     * @param $endDay
     */
    private function getCourseEndTime($courseId, $endDay) {
        if ($endDay !== '0000-00-00 00:00:00')
            return $endDay;

        $classIds = getCourseClassIds($courseId);

        $realEndTime = $endDay;
        foreach ($classIds as $classId) {
            $class = getLiveClassInfo($classId);

            //开始时间和结束时间设置
            $realEndTime = strcmp($class->end_time, $realEndTime) > 0 ? $class->end_time : $realEndTime;
        }

        return $realEndTime;
    }
}
